package com.example.mybatis01basic.controller;

import com.example.mybatis01basic.entity.dto.InsertEmployeeDto;
import com.example.mybatis01basic.entity.po.Employee;
import com.example.mybatis01basic.service.EmployeeService;
import liquibase.pro.packaged.E;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
实现 mybatis 最基础的 增删改查
 */

@RestController
@RequestMapping("/employee")
public class EmployeeController {


    @Autowired
    EmployeeService employeeService;


    /*
    * 1. 新增一个数据
    * */
    @PostMapping
    public Boolean insertOneEmployee(@RequestBody Employee employee){
        return employeeService.save(employee);
    }


    /*
    * 2. 删除一条数据
    * */
    @DeleteMapping
    public Boolean deleteOneById(@RequestParam Integer id){
        return employeeService.removeById(id);
    }

    /*
    * 3. 修改一条数据
    * */

    @PutMapping
    public Boolean updateById(@RequestBody Employee employee){
        return employeeService.updateById(employee);
    }



    /*
    * 4. 查询全部数据
    * */

    @GetMapping
    public List<Employee> getAllEmployee(){
        return employeeService.list(null);
    }

    /*
    * 5. 根据ID获取数据
    *
    * */
    @GetMapping("id")
    public Object getEmployeeById(Integer id){
        final Employee employee = employeeService.getById(id);
        if (employee == null) {
            return "没有查到数据";
        }
        return employee;
    }
}
